﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using webui.Models;
using BLL;
using SqlSugar;
using Models;
using System.Data;
using Models.ApiModel;

namespace webui.Areas.Admin.Controllers
{
    public class CaclPriceController : Controller
    {
        // GET: Admin/CaclPrice
        public ActionResult Index()
        {
            ViewBag.loginname = OperateContext.Current.Usr.loginname;
            return View();
        }

        [HttpPost]
        [Skip]
        public ActionResult DataList(PageRequst req)
        {
            int pageIndex = req.page == 0 ? 1 : req.page;
            int pageSize = req.rows == 0 ? 1 : req.rows;
            Car_carInfoBLL carbll = new Car_carInfoBLL();
         
            List<IConditionalModel> liswhere = new List<IConditionalModel>();
            List<Car_carInfo> list = new List<Car_carInfo>();
            string sql = "SELECT sellUserName,SUM(DudectMoney) AS 'DudectMoney'  FROM dbo.Car_carInfo WHERE sellState=3";
            liswhere.Add(new ConditionalModel() { FieldName = "sellState", ConditionalType = ConditionalType.Equal, FieldValue = ((int)BaseNeum.selled).ToString() });
            if ( OperateContext.Current.Usr.loginname=="admin")
            {
                if (req.Shopid!=0)
                {
                    liswhere.Add(new ConditionalModel() { FieldName = "Shopid", ConditionalType = ConditionalType.Equal, FieldValue = req.Shopid.ToString() });
                    sql += " and shopid=" + req.Shopid;
                }
            }
            else
            {
                liswhere.Add(new ConditionalModel() { FieldName = "Shopid", ConditionalType = ConditionalType.Equal, FieldValue = OperateContext.Current.Usr.Shopid.ToString() });
                sql += " and shopid=" + OperateContext.Current.Usr.Shopid;
            }
 
            if (!string.IsNullOrEmpty(req.BeginTiem))
            {
                liswhere.Add(new ConditionalModel() { FieldName = "CloseTime", ConditionalType = ConditionalType.GreaterThanOrEqual, FieldValue = req.BeginTiem });
                sql += "  AND CloseTime>='" + req.BeginTiem + "' ";
            }
            if (!string.IsNullOrEmpty(req.EndTime))
            {
                liswhere.Add(new ConditionalModel() { FieldName = "CloseTime", ConditionalType = ConditionalType.LessThanOrEqual, FieldValue = req.EndTime });
                sql += "  AND CloseTime<='" + req.EndTime + "' ";
            }
            sql += "   GROUP BY sellUserName";
            list =   carbll.GetList(liswhere);
            DataTable dt= carbll.GetTableBySQL(sql, null);
            foreach (DataRow item in dt.Rows)
            {
                Car_carInfo car = new Car_carInfo() ;
                car.sellUserName = item["sellUserName"].ToString()+ "：合计" ;
                car.DudectMoney = decimal.Parse(item["DudectMoney"].ToString());
                car.regdate = null;
                list.Add(car);
            }
            return Json(new DataGridModel() { rows = list, total =list.Count });
        }

    }
}